package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.pojo.Pages;
import com.subaiwang.service.plastic.PlasticService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/plastic")
public class PlasticController {

    @Resource
    private PlasticService plasticService;

    /**
     * 塑料管理  --塑料类表
     * @return
     */

    @RequestMapping("/Plastic_list.html")
    public String products_List(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=plasticService.getCount(null,null);
        pages.setPageCount(count);
        plasticService.getPlastic(null,null,pages);

        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "Plastic_list";
    }

    /**
     *  塑料管理  --塑料类表  分页显示
     * @param session
     *
     * @param pageNo
     * @return
     */

    @RequestMapping(value = "/PlasticListajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost(HttpSession session,
                         @RequestParam(value = "typename",required = false) String typename,
                         @RequestParam(value = "brand",required = false) String brand,
                         @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=plasticService.getCount(typename,brand);
        if (typename.equals("0")){
            count=plasticService.getCount(null,brand);
            plasticService.getPlastic(null,brand,pages);
        }else{
            count=plasticService.getCount(typename,brand);
            plasticService.getPlastic(typename,brand,pages);
        }
        pages.setPageCount(count);

        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }
}
